﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Data.Objects.DataClasses;

namespace SilverlightSdms.Web.Services
{
    [ServiceContract(Namespace = "")]
	[AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)]
	public class FacilitiesService
	{
        [OperationContract]
        public List<FacilitieInfo> GetFacilities()
        {
            using (var context = new SdmsEntities())
            {
                return (
                    from school in context.Schools
                    select new FacilitieInfo
                    {
                        FacilitieId = school.Id,
                        Name = school.Name,
                        Address = school.Address,
                        Region = school.Region.Name
                    }
                    ).ToList<FacilitieInfo>();
            }
        }

        [OperationContract]
        public List<FacilitieInfo> GetFacilitiesByUser(int UserId)
        {
            using (var context = new SdmsEntities())
            {
                return (
                    from region in context.Regions
                    from teamMember in context.TeamMembers
                    from school in context.Schools
                    where region.Team.Id == teamMember.Team.Id
                    && teamMember.User.Id == UserId
                    && region.Schools.Contains(school)
                    select new FacilitieInfo
                    {
                        FacilitieId = school.Id,
                        Name = school.Name,
                        Address = school.Address,
                        Region = school.Region.Name
                    }
                    ).ToList<FacilitieInfo>();
            }
        }

        [OperationContract]
        public List<FacilitieInfo> GetFacilitiesForTeamMember(int teamLeaderId)
        {
            List<FacilitieInfo> schools = new List<FacilitieInfo>();

            using (var context = new SdmsEntities())
            {
                var teams = context.Teams.Where(t => t.TeamLeaderId == teamLeaderId);

                if (teams != null && teams.Count() > 0)
                {
                    Team team = teams.First();

                    foreach (var item in team.Regions)
                    {
                        foreach (var school in item.Schools)
                        {
                            schools.Add(new FacilitieInfo
                                {
                                    FacilitieId = school.Id,
                                    Name = school.Name,
                                    Address = school.Address,
                                    Region = school.Region.Name
                                });
                        }
                    }
                }
            }

            return schools;
        }

		// Add more operations here and mark them with [OperationContract]
	}

    public class FacilitieInfo
    {
        public int FacilitieId { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public string Region { get; set; }

        public FacilitieInfo() { }

        public FacilitieInfo(int FacilitieId, string Name, string Address, string Region)
        {
            this.FacilitieId = FacilitieId;
            this.Name = Name;
            this.Address = Address;
            this.Region = Region;
        }
    }
}
